package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.adpater.RouteManager;
import com.xyy.saas.payment.callback.core.job.SyncFMRefundStatusJob;
import com.xyy.saas.payment.cores.enums.BalanceAccountTypeEnum;
import com.xyy.saas.payment.cores.vo.QueryAccountBalanceDO;
import com.xyy.saas.payment.adpater.impl.FuminAdapter2;
import com.xyy.saas.payment.api.impl.PaymentMerchantApiImpl;
import com.xyy.saas.payment.api.impl.PaymentOpenAccountApiImpl;
import com.xyy.saas.payment.api.impl.PaymentOrderApiImpl;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.AccountChannelEnum;
import com.xyy.saas.payment.cores.param.MerchantAccountModifyParam;
import com.xyy.saas.payment.cores.param.PersonalAccountModifyParam;
import com.xyy.saas.payment.cores.param.QueryAccountModifyParam;
import com.xyy.saas.payment.cores.vo.*;
import com.xyy.saas.payment.merchant.core.dto.*;
import com.xyy.saas.payment.merchant.core.service.UserInfoService2;
import com.xyy.saas.payment.payment.core.dto.*;
import com.xyy.saas.payment.settle.core.service.AccountService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping(value = {"/api/fbank/v2/"})
public class FuminAdapter2TestController {
    @Resource
    private PaymentOrderApiImpl paymentOrderApi;
    @Resource
    private PaymentMerchantApiImpl paymentMerchantApi;
    @Resource
    private PaymentOpenAccountApiImpl paymentOpenAccountApi;
    @Autowired
    private AccountService accountService;
    @Resource
    private RouteManager routeManager;
    @Resource
    private FuminAdapter2 fuminAdapter2;
    @Resource
    private UserInfoService2 userInfoService2;
    @Resource
    private SyncFMRefundStatusJob syncFMRefundStatusJob;

    @RequestMapping(value = "/uploadPicture", method = RequestMethod.POST)
    public String uploadPicture(String merchantNo, String url) throws PaymentException {
        return fuminAdapter2.uploadPicture(merchantNo, url);
    }

    @RequestMapping(value = "/operate51WhiteList", method = RequestMethod.POST)
    public Object operate51WhiteList(@RequestBody FBank51WhiteList fBank51WhiteList) throws PaymentException {
        return fuminAdapter2.operate51WhiteList(fBank51WhiteList);
    }

    @RequestMapping(value = "/queryUserInfo", method = RequestMethod.POST)
    public Object queryUserInfo(String accountId) throws Exception {
        return userInfoService2.queryUserInfo(accountId);
    }
    /**
     * 提现
     */
    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    public Object withdraw(@RequestBody OrderWithdrawVo orderWithdrawVo) throws PaymentException {
        return paymentOrderApi.withdraw(orderWithdrawVo, StringUtils.EMPTY);
    }

    /**
     * 提现鉴权
     */
    @RequestMapping(value = "/withdrawAuth", method = RequestMethod.POST)
    public Object withdrawAuth(@RequestBody WithdrawAuthVo withdrawAuthVo) throws PaymentException {
        return paymentOrderApi.withdrawAuth(withdrawAuthVo, StringUtils.EMPTY);
    }

    /**
     * 企业开户
     */
    @RequestMapping(value = "/createMerchant", method = RequestMethod.POST)
    public Object createMerchant(@RequestBody MerchantAccountCreateVo param) {
        return paymentMerchantApi.merchantCertifyAccountCreate(param, StringUtils.EMPTY);
    }

    /**
     * 企业开户修改
     */
    @RequestMapping(value = "/modifyMerchant", method = RequestMethod.POST)
    public Object modifyMerchant(@RequestBody MerchantAccountModifyParam param) throws PaymentException {
        return paymentOpenAccountApi.modifyMerchant(param, StringUtils.EMPTY);
    }

    /**
     * 查询企业开户信息
     */
    @RequestMapping(value = "/queryModifyMerchant", method = RequestMethod.POST)
    public Object queryModifyMerchant(@RequestBody QueryAccountModifyParam param) throws Exception {
        return paymentOpenAccountApi.queryModifyMerchant(param, StringUtils.EMPTY);
    }

    /**
     * 查询企业开户信息
     */
    @RequestMapping(value = "/queryModifyMerchantList", method = RequestMethod.POST)
    public Object queryModifyMerchantList(@RequestBody QueryAccountModifyParam param) throws Exception {
        return paymentOpenAccountApi.queryModifyMerchantList(param, StringUtils.EMPTY);
    }

    /**
     * 个人开户
     */
    @RequestMapping(value = "/createPersonal", method = RequestMethod.POST)
    public Object createPersonal(@RequestBody PersonalAccountCreateVo userDto) throws PaymentException {
        return paymentMerchantApi.personalCertifyAccountCreate(userDto, StringUtils.EMPTY);
    }

    /**
     * 个人开户修改
     */
    @RequestMapping(value = "/modifyPersonal", method = RequestMethod.POST)
    public Object modifyPersonal(@RequestBody PersonalAccountModifyParam param) throws PaymentException {
        return paymentOpenAccountApi.modifyPersonal(param, StringUtils.EMPTY);
    }

    /**
     * 企业换绑
     */
    @RequestMapping(value = "/merchantSettleCardReBind", method = RequestMethod.POST)
    public Object merchantSettleCardReBind(@RequestBody MerchantSettleCardBindVo param) {
        return paymentMerchantApi.merchantSettleCardReBind(param, StringUtils.EMPTY);
    }

    /**
     * 个人换绑
     */
    @RequestMapping(value = "/personalSettleCardReBind", method = RequestMethod.POST)
    public Object personalSettleCardReBind(@RequestBody PersonalSettleCardBindVo param) {
        return paymentMerchantApi.personalSettleCardReBind(param, StringUtils.EMPTY);
    }

    /**
     * 查询账户余额
     */
    @RequestMapping(value = "/queryAccount", method = RequestMethod.POST)
    public Object queryAccount(String userId, String businessIdType) {
        return accountService.queryAccount(userId, businessIdType, AccountChannelEnum.AGGREGATE.getChannel(), BalanceAccountTypeEnum.FUND_ACCOUNT.getType());
    }

    /**
     * 发送短信验证码
     */
    @RequestMapping(value = "/sendActiveCodeByPhoneNum", method = RequestMethod.POST)
    public String sendActiveCodeByPhoneNum(@RequestBody ActiveCodeDto activeCodeDto) throws PaymentException {
        accountService.sendActiveCodeByPhoneNum(activeCodeDto);
        return "SUCCESS";
    }

    /**
     * 转账
     */
    @RequestMapping(value = "/separateAccountFromUser", method = RequestMethod.POST)
    public Object separateAccountFromUser(@RequestBody SeparateAccountFromUserVo separateAccountFromUserVo) {
        return paymentOrderApi.separateAccountFromUser(separateAccountFromUserVo, StringUtils.EMPTY);
    }

    /**
     * 查询账户流水
     */
    @RequestMapping(value = "/queryBalanceRecord", method = RequestMethod.POST)
    public Object queryBalanceRecord(@RequestBody QueryAccountBalanceDO queryAccountBalanceDO) throws PaymentException {
        return routeManager.queryBalanceRecord(queryAccountBalanceDO);
    }

    /**
     * 冻结
     */
    @RequestMapping(value = "/frozenBalance", method = RequestMethod.POST)
    public Object frozenBalance(@RequestBody FrozenDto frozenDto) throws PaymentException {
        return routeManager.frozenBalance(frozenDto);
    }

    /**
     * 解冻
     */
    @RequestMapping(value = "/unfrozenBalance", method = RequestMethod.POST)
    public Object unfrozenBalance(@RequestBody UnfrozenDto unfrozenDto) throws PaymentException {
        return routeManager.unfrozenBalance(unfrozenDto);
    }

    /**
     * 同步富民退款状态
     */
    @RequestMapping(value = "/syncFMRefundStatusJob", method = RequestMethod.POST)
    public Object syncFMRefundStatusJob(String refundNo, Byte status) {
        syncFMRefundStatusJob.execute(refundNo, status);
        return "SUCCESS";
    }
}
